package com.zls.mapper;

import com.zls.pojo.Branch;
import com.zls.pojo.Taskplan;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;

public interface TaskplanMapper {
    @Results(id = "TaskplanMap", value = {
            @Result(column = "tid", property = "tid"),
            @Result(column = "tname", property = "tname"),
            @Result(column = "starttime", property = "starttime"),
            @Result(column = "endtime", property = "endtime"),
            @Result(column = "updatetime", property = "updatetime"),
            @Result(column = "bid", property = "bid"),
            @Result(column = "bid", property = "branch", javaType = Branch.class, one =
                    //FetchType.LAZY 懒加载   EAGER:立即加载
                    //select 代表的关联查询  调用指定的mapper方法
            @One(select = "com.com.zls.mapper.BranchMapper.findByBid", fetchType = FetchType.EAGER))
    })
    @Select("Select * from taskplan where status =1")
    List<Taskplan> findAll();

    @ResultMap("TaskplanMap")
    @Select("<script>" +
            "select * from taskplan where status =1" +
            "<if test='queryString!=null'> and tname like concat( '%',#{queryString},'%')" +
            "limit #{currentPage},#{pageSize}</if>" +
            "<if test='queryString==null'>limit #{currentPage},#{pageSize}</if> "+
            "</script>")
    List<Taskplan> findTask(@Param("currentPage") Integer currentPage, @Param("pageSize") Integer pageSize, @Param("queryString") String queryString);

    //查询总记录数
    @Select("<script>" +
            "select count(*) from taskplan where status =1" +
            "<if test='queryString!=null'> and tname like concat( '%',#{queryString},'%') </if>" +
            "</script>")
    Long count(@Param("queryString") String queryString);

    @Insert("insert into taskplan (tid,tname,starttime,endtime,updatetime,bid)" +
            "values (#{tid},#{tname},#{starttime},#{endtime},#{updatetime},#{bid})")
    int addTaskplan(Taskplan taskplan);

    //根据sid查询
//    @ResultMap("StageMap")
    @Select("select * from taskplan where status =1 and tid=#{tid}")
    Taskplan findByTid(String tid);

    //修改
    @Update("update taskplan set tname=#{tname},starttime=#{starttime},bid=#{bid},endtime=#{endtime},updatetime=#{updatetime} where tid=#{tid} and status =1")
    int updateTaskplan(Taskplan taskplan);

    //逻辑删除
    @Update("update taskplan set status=0 where tid=#{tid}")
    int deleteTaskplan(String tid);

    @Select("select * from taskplan where status =1 and bid=#{bid}")
    List<Taskplan> findByBid(String bid);
}
